<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>demo_sse</title>
  </head>
  <body>
    <h2>Server Send Event测试</h2>
    <ul></ul>
  </body>
  <script>
    const box = document.querySelector(".box");
    const eventSource = new EventSource("http://localhost:3000/sse");
    eventSource.onopen = () => {
      console.log("连接已经建立");
    };
    eventSource.onmessage = (res) => {
      console.log("获取的数据：" + res.data);
      var ulDom = document.querySelector("ul");
      var liDom = document.createElement("li");
      liDom.innerHTML = res.data;
      ulDom.appendChild(liDom);
    };
    eventSource.onerror = (err) => {
      console.log("连接已经断开");
    };
  </script>
</html>
